import com.robsondesign.charts.series.LabeledBubbleSeries;

import mx.charts.*;
import mx.charts.events.*;
import mx.charts.renderers.*;
import mx.charts.series.*;
import mx.collections.*;
import mx.utils.ArrayUtil;

import org.quantitativesustainability.dataStructures.AField;
import org.quantitativesustainability.events.EnableChangeEvent;
import org.quantitativesustainability.styles.Styles;


[Bindable]
public var dataProvider:ArrayCollection;
public var data_a:ArrayCollection;
public var years:AField=new AField();
public var indexList:Array=[];
public var ss:StyleSheet=new StyleSheet();
private var urlLoader:URLLoader;

public var styles:Styles=new Styles();

public function dataTipFunction(hd:HitData):String
{
    return hd.item.lField;
}

private var styleSheet:StyleSheet;

private function urlLoader_complete(evt:Event):void
{
    var css:String=URLLoader(evt.currentTarget).data;
    // Convert text to style sheet.
    styleSheet=new StyleSheet();
    styleSheet.parseCSS(css);
    // Set the style sheet.
    genInfoText.styleSheet=styleSheet;
    specInfoText.styleSheet=styleSheet;
}


public function init():void
{
    data_a=new ArrayCollection(ArrayUtil.toArray(data_m.fuel));
    years.setData(new ArrayCollection(ArrayUtil.toArray(data_m.aField)));
    years.label='Years';

    wdhs.init(years);
    //wdhs.hslider.addEventListener(SliderEvent.CHANGE, updateData);
    addEventListener(EnableChangeEvent.ENABLE_CHANGED, updateData);

    // Add data
    var s:BubbleSeries=makeBubbleSeries(data_a, 0);
    c.dataTipFunction=dataTipFunction;

    urlLoader=new URLLoader();
    urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
    urlLoader.load(new URLRequest("../assets/default.css"));
}

public function displayInfo(e:ChartItemEvent):void
{
    info.selectedIndex=1;
    specInfoText.htmlText=e.hitData.item.info;
}

public function makeBubbleSeries(data_a:ArrayCollection, index:int):BubbleSeries
{
    dataProvider=new ArrayCollection();
    for (var i:int=0; i < data_a.length; ++i)
    {
        var item:Object={'xField': data_a[i].price,
                'rField': data_a[i].generation.time[index],
                'yField': data_a[i].emissionFactor,
                'lField': data_a[i].name,
                'info': data_a[i].info
            }
        dataProvider.addItem(item);
    }

    //var s:BubbleSeries = new BubbleSeries();
    var s:LabeledBubbleSeries=new LabeledBubbleSeries();
    s.setStyle('textVAlign', 'middle');
    s.dataProvider=dataProvider;
    s.yField="yField";
    s.xField="xField";
    s.displayName="... display name here ...";
    s.radiusField="rField";
    s.labelField="lField";
    s.visible=true;
    s.maxRadius=100;
    s.setStyle('fill', styles.sc1);
    s.setStyle('stroke', styles.st1);

    return s;
}

public function updateData(e:EnableChangeEvent):void
{
    var series:Array=c.series;
    var found:int=indexList.indexOf(years.index);
    if (found == -1)
    {
        var s:BubbleSeries=makeBubbleSeries(data_a, years.index);
        indexList.push(years.index);
        series.push(s);
        found=series.length - 1;
    }
    else
    {
        series[found].labelField='lField';
        series[found].setStyle('fill', styles.sc1);
        series[found].setStyle('stroke', styles.st1);
    }
    for (var l:int=0; l < series.length; ++l)
    {
        if (l == found)
            continue;
        series[l].labelField='';
        series[l].setStyle('fill', styles.sc2);
        series[l].setStyle('stroke', styles.st2);
    }
    c.series=series;
}

